<?php
/**
 * Created by PhpStorm.
 * User: fangwei
 * Date: 2019-06-23
 * Time: 21:13
 */

namespace App\Http\Repositories\Services\Impls;


use App\Exceptions\ServiceException;
use App\Http\Constants\CommonExceptionConstants;
use App\Http\Constants\ServiceExceptionConstants;
use App\Http\Repositories\Daos\IPermissionDao;
use App\Http\Repositories\Eloquent\Permission;
use App\Http\Repositories\Services\IPermission;
use App\Http\Requests\Admin\PermissionInputDTO;
use App\Util\Helper;

class PermissionService implements IPermission
{
    protected $permissionDao;

    public function __construct(IPermissionDao $permissionDao)
    {
        $this->permissionDao  = $permissionDao;
    }

    public function byId($id)
    {
        $dao = $this->permissionDao->find($id);
        if (!$dao) {
            throw new ServiceException(CommonExceptionConstants::getKey('no_find_data'));
        }
        return $dao;
    }

    public function update(PermissionInputDTO $inputDTO, $id)
    {
        $permission = Permission::findOrFail($id);
        if (!$permission) {
            throw new ServiceException(CommonExceptionConstants::getKey('no_find_data'));
        }
        $data = [];
        $data['name'] = $inputDTO->get('name');
        $data['display_name'] = $inputDTO->get('display_name');
        $data['guard_name'] = $inputDTO->get('guard_name');
        $data['route'] = $inputDTO->get('route');
        $data['icon'] = $inputDTO->get('icon');
        $data['parent_id'] = $inputDTO->get('parent_id');
        $data['sort'] = $inputDTO->get('sort');
        $data['note'] = $inputDTO->get('note');

        $permission->update($data);
    }

    public function store(PermissionInputDTO $inputDTO)
    {
        $data = [];
        $data['name'] = $inputDTO->get('name');
        $data['display_name'] = $inputDTO->get('display_name');
        $data['guard_name'] = $inputDTO->get('guard_name');
        $data['route'] = $inputDTO->get('route');
        $data['icon'] = $inputDTO->get('icon');
        $data['parent_id'] = $inputDTO->get('parent_id');
        $data['sort'] = $inputDTO->get('sort');
        $data['note'] = $inputDTO->get('note');
        Permission::create($data);
    }

    public function delete($id)
    {
        $dao = $this->permissionDao->find($id);
        if (!$dao) {
            throw new ServiceException(CommonExceptionConstants::getKey('no_find_data'));
        }

        if ($this->permissionDao->exists(['is_deleted'=>0,'parent_id'=>$id])) {
            throw new ServiceException(ServiceExceptionConstants::getKey('is_lower_level_permission'));
        }

        $dao->is_deleted = 1;
        $dao->save();
    }

    public function state($id, $state)
    {
        $dao = $this->permissionDao->find($id);
        if (!$dao) {
            throw new ServiceException(CommonExceptionConstants::getKey('no_find_data'));
        }
        $dao->is_show = $state;
        $dao->save();
    }

    public function getTree($pid=0,$data=[]){
        $search = [];
        $search['parent_id'] = $pid;
        $search['is_show'] = '1';
        $dao = $this->permissionDao->getAll($search);

        if ($dao) {
            $permission = [];
            foreach ($dao as $key=>$val){
                $permission[$key]['id'] = $val->id;
                $permission[$key]['name'] = $val->name;
                $permission[$key]['icon'] = $val->icon;
                $permission[$key]['route'] = $val->route;
                $permission[$key]['title'] = $val->display_name;
                $permission[$key]['guard_name'] = $val->guard_name;
                $permission[$key]['spread'] = true;
                $permission[$key]['children'] = $this->getTree($val->id,$data);
             }
            $data = $permission;
        }
        return $data;
    }

    public function getAll()
    {

        $dao = $this->permissionDao->getAll([])->toArray();
        $data = [];
        if ($dao) {
            Helper::rLevel('id',$dao,$data);
        }
        $dao = [];
        $i = 0 ;
        foreach ($data as $key=>$val) {
            $dao[$i] = $val;
            $i++;
        }
        return $dao;
    }

}